﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace DARE
{
    public class CVideoConfig
    {

    #region fields

        private CDare m_dare;
        private int m_resolutionWidth;
        private int m_resolutionHeight;
        private bool m_unlockFps;
        private bool m_verticalSync;
        private bool m_multisampling;
        private bool m_mouseVisible;
        private bool m_fullscreen;
        private Color m_clearColor;
        private bool m_autoApplyChanges;
        private bool m_configChanged;
        private bool m_resolutionChanged;

    #endregion

    #region properties

        public CDare Dare
        {
            get { return m_dare; }
        }

        public Vector2 WindowRatio
        {
            get;
            set;
        }

        public bool AutoApplyChanges
        {
            get { return m_autoApplyChanges; }
            set { m_autoApplyChanges = value; }
        }

        public int ResolutionWidth
        {
            get { return m_resolutionWidth; }
            set 
            { 
                m_resolutionWidth = m_dare.GraphicsDeviceManager.PreferredBackBufferWidth = value;
                m_resolutionChanged = true;
                Changed();
            }
        }

        public int ResolutionHeight
        {
            get { return m_resolutionHeight; }
            set 
            {
                m_resolutionHeight = m_dare.GraphicsDeviceManager.PreferredBackBufferHeight = value;
                m_resolutionChanged = true;
                Changed();
            }
        }

        public bool UnlockFps
        {
            get { return m_unlockFps; }
            set 
            { 
                m_dare.Game.IsFixedTimeStep = !value; 
                m_unlockFps = !m_dare.Game.IsFixedTimeStep;
            }
        }

        public bool VerticalSync
        {
            get { return m_verticalSync; }
            set 
            { 
                m_verticalSync = m_dare.GraphicsDeviceManager.SynchronizeWithVerticalRetrace = value; 
                Changed();
            }
        }

        public bool FullScreen
        {
            get { return m_fullscreen; }
            set 
            { 
                m_fullscreen = m_dare.GraphicsDeviceManager.IsFullScreen = value; 
                Changed();
            }
        }

        public bool MultiSampling
        {
            get { return m_multisampling; }
            set 
            { 
                m_multisampling = m_dare.GraphicsDeviceManager.PreferMultiSampling = value; 
                Changed();
            }
        }

        public Color ClearColor
        {
            get { return m_clearColor; }
            set { m_clearColor = value; }
        }

    #endregion

    #region ctor

        public CVideoConfig()
        {
            Initialize(CDare.Instance);
        }

        public CVideoConfig(CDare dare)
        {
            Initialize(dare);
        }

        private void Initialize(CDare dare)
        {
            m_dare = dare;
            SetResolution(1024, 768);
            UnlockFps = true;
            VerticalSync = false;
            FullScreen = false;
            MultiSampling = false;
            m_clearColor = Color.Black;
            m_autoApplyChanges = true;
            m_configChanged = false;
            m_resolutionChanged = false;
        }

    #endregion

    #region methods

        public void CalculateRatio()
        {
            this.WindowRatio = new Vector2(((float) this.ResolutionWidth / (float) this.Dare.Game.Window.ClientBounds.Width), ((float) this.ResolutionHeight / (float) this.Dare.Game.Window.ClientBounds.Height));
            if (this.Dare.DebugMgr != null)
                this.Dare.DebugMgr.DebugConsole.WriteLine(string.Format("Windows Ratio : {0}", this.WindowRatio.ToString()), CDebugConsole.EMessageType.NORMAL);
        }

        public void SetResolution(int width , int height)
        {
            m_resolutionWidth = m_dare.GraphicsDeviceManager.PreferredBackBufferWidth = width;
            m_resolutionHeight = m_dare.GraphicsDeviceManager.PreferredBackBufferHeight = height;
            m_resolutionChanged = true;
            Changed();
            CalculateRatio();
        }

        public void ApplyChanges()
        {
            if (m_configChanged == true)
            {
                m_dare.GraphicsDeviceManager.ApplyChanges();
                if (m_resolutionChanged)
                {
                    if (m_dare.SceneMgr.ActiveScene != null && 
                        m_dare.SceneMgr.ActiveScene.ActiveRender3D != null && 
                        m_dare.SceneMgr.ActiveScene.ActiveRender3D.CameraMgr.ActiveCamera != null)
                        m_dare.SceneMgr.ActiveScene.ActiveRender3D.CameraMgr.ActiveCamera.UpdateAspectRatio();
                }
                m_resolutionChanged = false;
                m_configChanged = false;
            }
        }

        private void Changed()
        {
            if (m_autoApplyChanges)
            {
                m_dare.GraphicsDeviceManager.ApplyChanges();
                if (m_resolutionChanged)
                {
                    if (m_dare.SceneMgr.ActiveScene != null && 
                        m_dare.SceneMgr.ActiveScene.ActiveRender3D != null && 
                        m_dare.SceneMgr.ActiveScene.ActiveRender3D.CameraMgr.ActiveCamera != null)
                        m_dare.SceneMgr.ActiveScene.ActiveRender3D.CameraMgr.ActiveCamera.UpdateAspectRatio();
                    m_resolutionChanged = false;
                }
            }
            else
                m_configChanged = true;
        }

    #endregion

    }
}
